package com.wowangz.cms.dailyoperation.chargemanagenew.api;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.wowangz.cms.common.utils.SysConstant;
import com.wowangz.cms.component.datagrid.pojo.DataGrid;
import com.wowangz.cms.component.datagrid.pojo.DataResult;
import com.wowangz.cms.component.datagrid.pojo.DataStyle;
import com.wowangz.cms.component.datagrid.pojo.PangingParam;
import com.wowangz.cms.component.datagrid.utils.DataTypeConstant;
import com.wowangz.cms.component.datagrid.utils.PangingTools;
import com.wowangz.cms.dailyoperation.chargemanagenew.pojo.Charge;
import com.wowangz.cms.dailyoperation.chargemanagenew.service.ChargeCheckService;
import com.wowangz.cms.dailyoperation.chargemanagenew.service.ChargeService;
import com.wowangz.cms.syscore.IssTransactionContainer;
import com.wowangz.cms.syscore.api.BaseController;
import com.wowangz.cms.syscore.api.JSONFactory;
import com.wowangz.cms.syscore.utils.MessageResourcesUtils;
import com.wowangz.cms.syscore.utils.ServletUtils;

@Scope("prototype")
@Controller
public class ChargeCheckController extends BaseController{

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(ChargeCheckController.class);
	
	/**
	 * 点击菜单"收费复核"显示“收费复核”列表
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/dailyoperation/chargemanage/tochargeCheckInfoList.web")
	public String tochargeCheckInfoList(HttpServletRequest request, HttpServletResponse response, Model model) {
		
		return "/WEB-INF/jsp/dailyoperation/chargemanagenew/chargeInfoCheckList";
	}
	
	/**
	 * 打开“服务管理”列表加载列表数据
	 * @param request
	 * @param response
	 * @param model
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/dailyoperation/chargemanage/querychargeCheckListInfoDataGrid.json")
	public void querychargeCheckListInfoDataGrid(HttpServletRequest request, HttpServletResponse response, Model model) {

		PangingParam pangingParam = null;
		DataResult dataResult = new DataResult();
		DataGrid dataGrid = null;

		try {
			pangingParam = (PangingParam) ServletUtils.getParametersToBaseEntity(request, PangingParam.class);
			HashMap<String, Object> map = (HashMap) ServletUtils.getParametersToHashMap(request);
			dataGrid = new DataGrid();
			dataGrid.setPangingParam(pangingParam);

			map.put("startrow", dataGrid.getStartrow());
			map.put("endrow", dataGrid.getEndrow());
			map.put("rows", dataGrid.getRows());
			map.put("sort", dataGrid.getSort());
			map.put("order", dataGrid.getOrder());
			map.put("statusid", SysConstant.RecordStatus.VALID);
			map.put("checkstatus", SysConstant.CheckStatus.SAVE);
			map.put("modifyuserid", getUserContext().getLoginUser().getId());
			ChargeCheckService chargeCheckService = (ChargeCheckService) IssTransactionContainer.getInstance(ChargeCheckService.class, false);
			long resultCount = chargeCheckService.findResultCheckCount(map);
			List<Charge> resultList = chargeCheckService.findResultCheckList(map);

			DataStyle dataStyle = new DataStyle();
			dataStyle.setPageIndex(pangingParam.getPage());
			dataStyle.setDataCount(resultCount);
			dataStyle.setDataList(resultList);
			dataStyle.addDepict("id", DataTypeConstant.LONG);
			dataStyle.addDepict("serviceid", DataTypeConstant.LONG);
			dataStyle.addDepict("servicenum", DataTypeConstant.STRING);
			dataStyle.addDepict("servicename", DataTypeConstant.STRING);
			dataStyle.addDepictByFunction("isproposed", SysConstant.ProposedOrMonth.class, "getName", new Class[]{long.class});
			dataStyle.addDepict("proposedprice", DataTypeConstant.AMOUNT_2);
			dataStyle.addDepictByFunction("ismonth", SysConstant.ProposedOrMonth.class, "getName", new Class[]{long.class});
			dataStyle.addDepict("monthprice", DataTypeConstant.AMOUNT_2);
			dataStyle.addDepictByFunction("checkstatus", SysConstant.CheckStatus.class, "getName", new Class[]{long.class});
			dataResult = PangingTools.buildResultPagerInfo(dataStyle);

			if (pangingParam.getReportExportType() == 0) {
				response.getWriter().write(JSONFactory.toJSONString(dataResult, userContext));
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 跳转复核明细页面
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/dailyoperation/chargemanage/tochargeView.web")
	public String tochargeView(HttpServletRequest request, HttpServletResponse response, Model model) {
		ChargeService chargeService = (ChargeService)IssTransactionContainer.getInstance(ChargeService.class, false);
		HashMap<String,Object> hashMap = new HashMap<String, Object>();
		Charge charge = null;
		Charge chargeCheck = null;
		String id = request.getParameter("id");
		try {
			hashMap.put("id", id);
			hashMap.put("statusid", SysConstant.RecordStatus.VALID);
			charge = chargeService.queryChargeByID(hashMap);
			chargeCheck = chargeService.queryChargeCheckByID(hashMap);
			Long yes = SysConstant.ProposedOrMonth.Yes;
			model.addAttribute("yes",yes);
			model.addAttribute("charge",charge);
			model.addAttribute("chargeCheck",chargeCheck);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/WEB-INF/jsp/dailyoperation/chargemanagenew/chargeView";
	}
	
	/**
	 * 跳转复核页面
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/dailyoperation/chargemanage/tochargeCheckView.web")
	public String tochargeCheckView(HttpServletRequest request, HttpServletResponse response, Model model) {
		ChargeService chargeService = (ChargeService)IssTransactionContainer.getInstance(ChargeService.class, false);
		HashMap<String,Object> hashMap = new HashMap<String, Object>();
		Charge charge = null;
		Charge chargeCheck = null;
		String id = request.getParameter("id");
		try {
			hashMap.put("id", id);
			hashMap.put("statusid", SysConstant.RecordStatus.VALID);
			charge = chargeService.queryChargeByID(hashMap);
			chargeCheck = chargeService.queryChargeCheckByID(hashMap);
			Long yes = SysConstant.ProposedOrMonth.Yes;
			model.addAttribute("yes",yes);
			model.addAttribute("charge",charge);
			model.addAttribute("chargeCheck",chargeCheck);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/WEB-INF/jsp/dailyoperation/chargemanagenew/chargeCheckView";
	}
	
	/**
	 * 复核
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/dailyoperation/chargemanage/checkcharge.web", method = { RequestMethod.GET, RequestMethod.POST })
	public String checkcharge(HttpServletRequest request, HttpServletResponse response, Model model) {
		ChargeService chargeService = (ChargeService)IssTransactionContainer.getInstance(ChargeService.class, false);
		Charge charge = (Charge)ServletUtils.getParametersToBaseEntity(request, Charge.class);
		HashMap<String,Object> hashMap = new HashMap<String,Object>();
		try{
			
			//更新临时表所有数据
			hashMap.put("id", charge.getId());
			hashMap.put("statusid", SysConstant.RecordStatus.VALID);
			hashMap.put("checkstatus", SysConstant.CheckStatus.CHECKED);
			hashMap.put("modifyuserid", getUserContext().getLoginUser().getId());
			hashMap.put("modifytime", new Date());
			chargeService.updatechargemanageTempByMap(hashMap);
			//更新主表数据状态
			hashMap.put("isproposed", charge.getIsproposed());
			hashMap.put("proposedprice", charge.getProposedprice());
			hashMap.put("ismonth", charge.getIsmonth());
			hashMap.put("monthprice", charge.getMonthprice());
			hashMap.put("datastatus", SysConstant.DataStatus.CHECKED);
			chargeService.updatechargemanageByMap(hashMap);
			
			userContext.setInfoMessage(MessageResourcesUtils.getMessage(request, "alertinfo.common.java.alert8"));
			userContext.setCloseWindow(true);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return "/WEB-INF/jsp/dailyoperation/chargemanagenew/chargeInfoCheckList";
	}
}
